package net.imwork.amorgemini.core.config.mybatisDatasourceconfig;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * Created by lvbr on 2017-09-06 21:55.
 */
@Configuration
@MapperScan(basePackages = {"net.imwork.amorgemini.mapper.mysql"}, sqlSessionFactoryRef = "sqlSessionFactoryPrimary")
public class MybatisPrimaryDataSourceConfig {

    @Autowired
    @Qualifier("mybatisPrimaryDataSource") // 注入数据源类
    private DataSource primaryDataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(primaryDataSource);
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplatePrimary() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryPrimary()); // 使用上面配置的Factory
        return template;
    }
}
